﻿using Microsoft.AspNet.Mvc.Formatters;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace ldhWebApiIIS
{
    public class TextPlainInputFormatter : InputFormatter
    {
        public override Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
        {
            if (context.HttpContext.Request.ContentType == "text/plain")
            {
                using (var reader = new StreamReader(context.HttpContext.Request.Body))
                {
                    return InputFormatterResult.SuccessAsync(reader.ReadToEnd());
                }
            }
            else
            {
                return InputFormatterResult.FailureAsync();
            }
        }

        public override bool CanRead(InputFormatterContext context)
        {
            return true;
        }
    }
}
